package com.czmh.etc.onlinepubsys.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czmh.etc.onlinepubsys.dto.CmsQueryContentParam;
import com.czmh.etc.onlinepubsys.maper.CmsContentMapper;
import com.czmh.etc.onlinepubsys.model.CmsContent;
import com.czmh.etc.onlinepubsys.services.CmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 资讯信息表 服务实现类
 * </p>
 *
 * @author eric
 * @since 2021-06-07
 */
@Service
public class CmsContentServiceImpl extends ServiceImpl<CmsContentMapper, CmsContent> implements CmsContentService {
    @Autowired
    CmsContentMapper cmsContentMapper;

    @Override
    public IPage<CmsContent> queryList(Long cmsContentCategoryId, Integer pageSize, Integer pageNum) {
        Page<CmsContent> page = new Page<>(pageNum,pageSize);
        QueryWrapper<CmsContent> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<CmsContent> lambda = wrapper.lambda();
        lambda.eq(CmsContent::getDeleteFlag, 0);
        lambda.eq(CmsContent::getShowStatus, 1);
        lambda.eq(CmsContent::getCategoryId, cmsContentCategoryId);
        Date currentDate = new Date();
        lambda.le(CmsContent::getBeginTime, currentDate);
        lambda.ge(CmsContent::getEndTime, currentDate);
        lambda.orderByDesc(CmsContent::getSort);
        return page(page, wrapper);
    }

    @Override
    public IPage<CmsContent> queryList(CmsQueryContentParam queryParam, Integer pageSize, Integer pageNum) {
        IPage<CmsContent> page = new Page<>(pageNum, pageSize);
        QueryWrapper<CmsContent> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<CmsContent> lambda = wrapper.lambda();
        if (queryParam.getId() != null) {
            lambda.eq(CmsContent::getId, queryParam.getId());
        }
        if (queryParam.getKeyword() != null) {
            lambda.and(qw -> qw.like(CmsContent::getContent, queryParam.getKeyword()).or().like(CmsContent::getTitle, queryParam.getKeyword()));
        }
        if (queryParam.getDeleteFlag() != null) {
            lambda.eq(CmsContent::getDeleteFlag, queryParam.getDeleteFlag());
        }
        if (queryParam.getShowStatus() != null) {
            lambda.eq(CmsContent::getShowStatus, queryParam.getShowStatus());
        }
        if (queryParam.getCategoryId() != null) {
            lambda.eq(CmsContent::getCategoryId, queryParam.getCategoryId());
        }
        Date currentDate = new Date();
        lambda.ge(CmsContent::getBeginTime, currentDate);
        lambda.le(CmsContent::getEndTime, currentDate);
        return page(page, wrapper);
    }
}
